package com.nantian.nfcm.bms.report.service;

import com.nantian.nfcm.bms.loan.dao.LoanAssignReportDao;
import com.nantian.nfcm.bms.loan.entity.LoanAssignReport;
import com.nantian.nfcm.bms.report.vo.LoanAssignReportBean;
import com.nantian.nfcm.util.DateUtil;
import com.nantian.nfcm.util.ServiceException;
import com.nantian.nfcm.util.vo.GridData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class LoanAssignReportService {
    private LoanAssignReportDao loanAssignReportDao;

    @Autowired
    public LoanAssignReportService(LoanAssignReportDao loanAssignReportDao) {
        this.loanAssignReportDao = loanAssignReportDao;
    }

    public GridData<LoanAssignReport> findByCondition(int page, int size, LoanAssignReportBean loanAssignReportBean)
            throws ServiceException {
        try{
            Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC,"loanId");
            Specification<LoanAssignReport> specification = (root, query, criteriaBuilder) -> {
                List<Predicate> predicates = new ArrayList<>();
                if (loanAssignReportBean.getBusinessType() != null && !loanAssignReportBean.getBusinessType().equals("")) {
                    Predicate businessType = criteriaBuilder.equal(root.get("businessType").as(String.class), loanAssignReportBean.getBusinessType());
                    predicates.add(businessType);
                }
                if (loanAssignReportBean.getLoanResult() != null && !loanAssignReportBean.getLoanResult().equals("")) {
                    Predicate loanResult = criteriaBuilder.equal(root.get("loanResult").as(String.class), loanAssignReportBean.getLoanResult());
                    predicates.add(loanResult);
                }
                if (loanAssignReportBean.getPrintFlag() != null && !loanAssignReportBean.getPrintFlag().equals("")) {
                    Predicate printFlag = criteriaBuilder.equal(root.get("printFlag").as(String.class), loanAssignReportBean.getPrintFlag());
                    predicates.add(printFlag);
                }
                String queryStartTime = loanAssignReportBean.getQueryStartTime();
                String queryEndTime = loanAssignReportBean.getQueryEndTime();
                if (queryStartTime != null && !queryStartTime.equals("")
                        &&queryEndTime != null && !queryEndTime.equals("") ) {
                    Predicate queryTime = criteriaBuilder.between(root.get("initTime").as(String.class),queryStartTime,queryEndTime);
                    predicates.add(queryTime);
                }

                query.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
                return query.getRestriction();
            };
            Page<LoanAssignReport> loanDetailedReportPage = loanAssignReportDao.findAll(specification, pageable);
            GridData<LoanAssignReport> gridData = new GridData<>();
            gridData.setData(loanDetailedReportPage.getContent());
            gridData.setNumber(loanDetailedReportPage.getTotalElements());
            gridData.setPage(loanDetailedReportPage.getNumber());
            gridData.setTotalPage(loanDetailedReportPage.getTotalPages());
            return gridData;
        }
        catch (Exception e){
            throw  new ServiceException(e.getMessage());
        }
    }

    @Transactional
    public void updateLoanAssignReport(List<LoanAssignReport> loanAssignReports){
        for (LoanAssignReport loanAssignReport:loanAssignReports){
            loanAssignReport.setPrintFlag("已打印");
            loanAssignReport.setLastPrintTime(DateUtil.getCurrentTime("yyyy-MM-dd hh:mm:ss"));
        }
        loanAssignReportDao.save(loanAssignReports);
    }
}
